﻿@{
    ViewData["Title"] = "UpdateCustomerDatatView";
    Layout = null;
}


<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />


    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">客户编码</label>
            <div class="layui-input-block">
                <input type="text" id="customerCode" name="customerCode" lay-verify="required" placeholder="请输入客户编码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">客户名称</label>
            <div class="layui-input-block">
                <input type="text" id="customerName" name="customerName" lay-verify="required" placeholder="请输入客户名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">客户简称</label>
            <div class="layui-input-block">
                <input type="text" id="customerNickName" name="customerNickName" lay-verify="required" placeholder="请输入客户简称" autocomplete="off" class="layui-input">
            </div>
        </div>
        
            <label class="layui-form-label">客户类型</label>
            <div class="layui-input-block">
                <select id="customerType" name="customerType" lay-verify="required">
                    <option value="企业客户">企业客户</option>
                    <option value="个人">个人</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">客户电话</label>
            <div class="layui-input-block">
                <input type="text" id="customerTel" name="customerTel" lay-verify="required" placeholder="请输入客户电话" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">联系人</label>
            <div class="layui-input-block">
                <input type="text" id="contact" name="contact" lay-verify="required" placeholder="请输入联系人" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">联系人-电话</label>
            <div class="layui-input-block">
                <input type="text" id="contactTel" name="contactTel" lay-verify="required" placeholder="请输入联系人电话" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input type="radio" name="isNot" value="true" title="是" checked>
                <input type="radio" name="isNot" value="false" title="否">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
            </div>
        </div>
    </form>

<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script src="~/libs/jquery/jquery.js"></script>

    <script>
        // 页面加载完成后执行的函数
        $(function () {
            mainUnitListOne();
            getMainUnitList();
        });

        // 获取主数据进行反填
        function mainUnitListOne() {
            console.log("获取客户数据");
            $.ajax({
                url: '/mainData/GetCustomerById',
                type: 'get',
                data: {
                    id: @ViewBag.Id
                },
                dataType: 'json',
                success: function (res) {
                    console.log(res.data);
                    $("#customerCode").val(res.data.customerCode);
                    $("#customerName").val(res.data.customerName);
                    $("#customerNickName").val(res.data.customerNickName);
                    $("#customerType").val(res.data.customerType);
                    $("#customerTel").val(res.data.customerTel);
                    $("#contact").val(res.data.contact);
                    $("#contactTel").val(res.data.contactTel);

                    // 设置 radio 选中状态
                    $('input[name="isNot"][value="' + res.data.isNot + '"]').prop('checked', true);

                    layui.form.render("select");
                    layui.form.render("radio");
                }
            });
        }

        // 获取主单位列表
        function getMainUnitList() {
            $.ajax({
                url: '/mainData/getPrimaryUnit',
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    console.log(res.data);
                    if (res.code == 0) {
                        var select = $('#primaryUnit'); // 获取 select 元素

                        // 清空现有的选项（除了第一个默认选项）
                        select.find('option:not(:first)').remove();

                        // 动态添加选项
                        $.each(res.data, function (key, value) {
                            select.append('<option value="' + value.id + '">' + value.customerName + '</option>');
                        });
                        layui.form.render('select');
                    }
                }
            });
        }

        layui.use(['form', 'laydate'], function () {
            var form = layui.form;
            var layer = layui.layer;

            // 监听是否启用的选择
            form.on('radio(isNot)', function (data) {
                var value = data.value; // 获取选中的值
                console.log(value);
                if (value === 'true') {
                    // 如果是启用，隐藏额外的字段
                    $('#mainUnitFields').hide();
                } else {
                    // 如果不是启用，显示额外的字段
                    $('#mainUnitFields').show();
                }
            });

            // 提交事件
            form.on('submit(demo1)', function (data) {
                console.log(data.field); // 获取表单字段值

                var field = data.field;
                field.id = @ViewBag.Id;
                field.isNot = field.isNot === 'true'; // 将字符串转换为布尔值

                console.log(field);
                $.ajax({
                    url: '/mainData/UpdateCustomerData',
                    type: 'post',
                    data: field,
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == true) {
                            layer.alert('修改成功！', function () {
                                closeWorkstation();
                            });
                        } else if (res.code == false) {
                            layer.alert('修改失败！,编号或名称已存在！');
                        }
                    }
                });
                console.log(1111111);
                return false; // 阻止默认 form 跳转
                
            });
        });

        // 关闭弹框
        function closeWorkstation() {
            var index = parent.layer.getFrameIndex(window.name); // 先得到当前的索引
            parent.layer.close(index); // 再执行关闭
        }
    </script>




